﻿using System.Collections.Generic;
using System.Threading.Tasks;
using NReader.Model;
using NReader.Model.RAS;
using Windows.UI.Xaml.Media.Imaging;

namespace NReader.Service
{
    public class ArticleService
    {
        public static ArticleService Service;

        public static ArticleService Instance()
        {
            return Service ?? (Service = new ArticleService());
        }

        public async Task<ArticleInfo> GetArticle(int articleid)
        {
            var client = new RemoteArticleServiceClient();
            var article = await client.GetArticleAsync(articleid);
            return article;
        }

        public async Task<ArticleInfo> GetArticleFormLocal(int articleid)
        {
            return await Local.Service.LocalSqliteService.Instance().GetArticle(articleid);
        }

        /// <summary>
        /// 获得每个书架现在的书籍数量
        /// </summary>
        /// <returns></returns>
        public async Task<int> GetBookStoreCountFromLocal(int bookcaseid)
        {
            return await Local.Service.LocalSqliteService.Instance().GetBookStoreCount(bookcaseid);
        }

        public async Task<bool> AddBookStoreToLocal(int articleid,int bookcaseid)
        {
            return await Local.Service.LocalSqliteService.Instance().AddBookStore(articleid, bookcaseid);
        }

        public async Task<IList<ChapterInfo>> GetMaxChapterList(int articleid)
        {
            var client = new RemoteArticleServiceClient();
            var list = await client.GetMaxChapterListAsync(articleid);
            return list;
        }

        public async Task<IList<ArticleInfo>> GetArticleListBySort(int sortid, int begin, int count)
        {
            var client = new RemoteArticleServiceClient();
            var list = await client.GetArticleListBySortAsync(sortid, begin, count);
            return list;
        }

        public async Task<int> GetArticleListCountBySort(int sortid)
        {
            var client = new RemoteArticleServiceClient();
            var count = await client.GetArticleListCountBySortAsync(sortid);
            return count;
        }

        public async Task<IList<ArticleInfo>> SearchArticleList(string key, int begin, int count)
        {
            var client = new RemoteArticleServiceClient();
            var list = await client.SearchArticleListAsync(key, begin, count);
            return list;
        }

        public async Task<int> SearchArticleListCount(string key)
        {
            var client = new RemoteArticleServiceClient();
            var count = await client.SearchArticleListCountAsync(key);
            return count;
        }

        public async Task<ChapterInfo> GetChapter(int chapterid)
        {
            var client = new RemoteArticleServiceClient();
            var result = await client.GetChapterAsync(chapterid);
            return result;
        }

        /// <summary>
        /// 将书籍信息添加到本地
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public async Task<bool> AddBook(ArticleInfo article)
        {
            return await Local.Service.LocalSqliteService.Instance().AddBook(article);
        }

        /// <summary>
        /// 从本地获取书籍信息
        /// </summary>
        /// <param name="articleid"></param>
        /// <returns></returns>
        public async Task<ArticleInfo> GetBookLocal(int articleid)
        {
            return await Local.Service.LocalSqliteService.Instance().GetArticle(articleid);
        }

        /// <summary>
        /// 更新本地书籍信息
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public async Task<bool> UpdateBook(ArticleInfo article)
        {
            return await Local.Service.LocalSqliteService.Instance().UpdateBook(article);
        } 

        /// <summary>
        /// 批量从本地删除书架书籍
        /// </summary>
        /// <param name="articleids"></param>
        /// <returns></returns>
        public async Task<bool> DeleteBookStore(string articleids)
        {
            return await Local.Service.LocalSqliteService.Instance().DeleteBookStore(articleids);
        }

        /// <summary>
        /// 从本地删除书架书籍,单笔删除数据库
        /// </summary>
        /// <param name="articleid"></param>
        /// <returns></returns>
        public async Task<bool> DeleteBookStore(int articleid)
        {
            return await Local.Service.LocalSqliteService.Instance().DeleteBookStore(articleid);
        }

        /// <summary>
        /// 从数据库中删除该书籍
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public async Task<bool> DeleteBook(ArticleInfo article)
        {
            return await Local.Service.LocalSqliteService.Instance().DeleteBook(article);
        }

        /// <summary>
        /// 从本地删除书架书籍,单笔删除文件
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public async Task<string> DeleteDocument(string filename)
        {
            return await Local.Service.LocalFileService.DeleteDocument(filename);
        }

        public async Task<BitmapImage> GetCover(int articleid)
        {
            return await Local.Service.LocalFileService.GetCover(articleid);
        }

        public async Task<BitmapImage> SaveCover(int articleid, byte[] bytes)
        {
            return await Local.Service.LocalFileService.SaveCover(articleid, bytes);
        }

        public async Task<IList<ArticleInfo>> GetBookStoreLocal(int bookcaseid)
        {
            return await Local.Service.LocalSqliteService.Instance().GetBookStore(bookcaseid);
        }

        /// <summary>
        /// 获取最大ID号，用以导入的本地文件ID
        /// </summary>
        /// <returns></returns>
        public async Task<int> GetArticleMaxID()
        {
            return await Local.Service.LocalSqliteService.Instance().GetMaxID(100000000);
        }

        public async Task<bool> ExistsArticle(string articlename)
        {
            return await Local.Service.LocalSqliteService.Instance().ExistsArticle(articlename);
        }

        /// <summary>
        /// 根据ID获取书架信息
        /// </summary>
        /// <param name="bookcaseid"></param>
        /// <returns></returns>
        public async Task<BookcaseInfo> GetBookcase(int bookcaseid)
        {
            return await Local.Service.LocalSqliteService.Instance().GetBookcase(bookcaseid);
        }

        /// <summary>
        /// 获取导入的文件内容
        /// </summary>
        /// <param name="filename">带后缀名的文件名</param>
        /// <param name="pos">读取的开始位置,比例10%</param>
        /// <param name="len">需要读取的长度</param>
        /// <param name="encoding">文件编码</param>
        /// <returns></returns>
        public async Task<FileDxInfo> GetImportContent(string filename, double pos, int len, string encoding)
        {
            return await Local.Service.LocalFileService.GetDocumentContent(filename, pos, len, encoding);
        }
    }
}
